﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OSGeo.MapGuide;
using System.Globalization;
using System.Collections.Specialized;

namespace IGIS
{
    public partial class quickplotpanel : utilityfunctions
    {
        public  int popup = 0;
      public  String clientWidth = "";
      public String mapName = "";
      public String sessionId = "";
      public String scale = "";
      public String centerX = "";
      public String centerY = "";
      public String dpi = "";
      public String locale = "";
      public String legend = "";
      public String layout = "";
     

        public common _comm =new common();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Charset = "utf-8";

            try
            {
                //fetch the parameters for this request
                //
                
                GetRequestParameters();

                /*MgLocalizer.SetLocalizedFilesPath(Request.ServerVariables["APPL_PHYSICAL_PATH"] + "..\\localized\\");
                String templ = MgLocalizer.Localize(_comm.LoadTemplate(Request, "../viewerfiles/Add-On/printablepageui.templ"), locale,_comm.GetClientOS(Request));
                */
                legend = GetLegendIcons();
              /*  String[] vals = {
            popup.ToString(NumberFormatInfo.InvariantInfo),
            clientWidth,
            sessionId,
            mapName,
            scale,
            centerX,
            centerY,
            dpi,
           _comm.GetSurroundVirtualPath(Request) + "printablepage.aspx",
            GetLegendIcons()};


                Response.Write(_comm.Substitute(templ, vals));*/
            }
            catch (Exception ne)
            {
                Response.Write(ne.Message);
            }

        }



        void GetRequestParameters()
        {
            if ("POST" == Request.HttpMethod)
            {
                GetParameters(Request.Form);
            }
            else
            {
                GetParameters(Request.QueryString);
            }
        }

        void GetParameters(NameValueCollection parameters)
        {
            locale = GetParameter(parameters, "LOCALE");
            if (_comm.IsParameter(parameters, "POPUP"))
            {
                popup = Convert.ToInt32(GetParameter(parameters, "POPUP"));
            }
            if (_comm.IsParameter(parameters, "WIDTH"))
            {
                clientWidth = GetParameter(parameters, "WIDTH");
            }
            if (_comm.IsParameter(parameters, "MAPNAME"))
            {
                mapName = GetParameter(parameters, "MAPNAME");
            }
            if (_comm.IsParameter(parameters, "SESSION"))
            {
                sessionId = GetParameter(parameters, "SESSION");
            }
            if (_comm.IsParameter(parameters, "SCALE"))
            {
                scale = GetParameter(parameters, "SCALE");
            }
            if (_comm.IsParameter(parameters, "CENTERX"))
            {
                centerX = GetParameter(parameters, "CENTERX");
            }
            if (_comm.IsParameter(parameters, "CENTERY"))
            {
                centerY = GetParameter(parameters, "CENTERY");
            }
            if (_comm.IsParameter(parameters, "DPI"))
            {
                dpi = GetParameter(parameters, "DPI");
            }

        }

        String GetLegendIcons()
        {
            // Initialize the web-tier.
            String realPath = Request.ServerVariables["APPL_PHYSICAL_PATH"];
            String configPath = realPath + "..\\webconfig.ini";
            MapGuideApi.MgInitializeWebTier(configPath);

            // Connect to the site.
            MgUserInformation userInfo = new MgUserInformation(sessionId);
            MgSiteConnection siteConnection = new MgSiteConnection();
            siteConnection.Open(userInfo);

            // Get an instance of the required service(s).
            MgResourceService resourceService = siteConnection.CreateService(MgServiceType.ResourceService) as MgResourceService;


            // Get the current map.
            MgMap map = new MgMap();
            map.Open(resourceService, mapName);

            // Get map layers.
            MgLayerCollection mgLayers = map.GetLayers();

            // Define table-body rows, one per layer.
            int mgLayerCount = mgLayers.Count;

            // Get the current-visiable layer list
            String jsonlist = "";
            for (int i = 0; i < mgLayerCount; i++)
            {
                MgLayer mgLayer = mgLayers.GetItem(i) as MgLayer;

                Boolean inLegend = false;
                String mgLayerName = mgLayer.LegendLabel;
                String mgLegendImage = mgLayer.GetLayerDefinition().Path + "/" + mgLayer.GetName();
                String mgLayerIsVisible = mgLayer.IsVisible() ? "on" : "off";
                String[] mgLayerData = new String[] { mgLegendImage, mgLayerName, mgLayerIsVisible };

                for (int j = 0; j < mgLayerData.Length - 1; j++)
                {
                    if (mgLayerData[mgLayerData.Length - 1] == "on" && mgLayerData[mgLayerData.Length - 2].Trim() != "")
                    {
                        jsonlist += mgLayerData[j] + "$";
                        inLegend = true;
                    }
                }

                if (inLegend)
                    jsonlist = jsonlist.Substring(0, (jsonlist.Length - 1)) + "@";
            }
            return jsonlist.Substring(0, (jsonlist.Length - 1));

        }
    }
}